# -*- coding: utf-8 -*-
__author__ = 'wwj'
import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
from celery import Celery
import config
import sys
import os
from celery.utils.log import get_task_logger
import pymongo
from datetime import *
# app = Celery('dispatch',include=['tasks.xvideo'])
app = Celery('dispatch')
app.config_from_object(config)
import hashlib
import youtube_dl
import requests
from subprocess import call
import scrapy
import re
import oss2
logger = get_task_logger(__name__)
OUTSTANAING = 0 #未处理 当添加一个新url时，其url状态。
PROCESSING = 1 #待处理 当url从队列中取出准备下载是，其url状态。
COMPLETE = 2 #已处理 当下载结束后，其url状态。
ERROR = 3 #处理失败 当下载失败时，其url状态。
from fdfs_client.client import *
client = Fdfs_client('/etc/fdfs/client.conf')
access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'LTAI1W2ci1bBerux')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '0ArWwWIBs7IMV3KdWq3RLRpXRZIHT5')
bucket_name = os.getenv('OSS_TEST_BUCKET', 'dmmvideo')
endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hongkong.aliyuncs.com')
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

class basetask(app.Task):
    client = pymongo.MongoClient("47.74.21.140",connect=False)
    client.admin.authenticate("admin321", "dsf::6666,,<<", mechanism='SCRAM-SHA-1')
    def on_success(self, retval, task_id, args, kwargs):   # 任务成功执行
        print args
        print kwargs
        # self.client.dmm2.img.update(
        #     {'url': args[0]},
        #     {'$set': kwargs}
        # )
        client.xvideo.video.update({'url': 11},{'$set': {'status': 111}})

    def on_failure(self, exc, task_id, args, kwargs, einfo):  #任务失败执行
        print args
        print kwargs
        # self.client.dmm2.img.update(
        #     {'url': args[0]},
        #     {'$set': {'status': ERROR, 'wait_time': datetime.now(),"filename":key}}
        # )

    def on_retry(self, exc, task_id, args, kwargs, einfo):    #任务重试执行
        # import ipdb;ipdb.set_trace()
        logger.info('task id:{} , arg:{} , retry !  einfo: {}'.format(task_id, args, exc))


@app.task(bind=True,base=basetask,queue="dmm",max_retries=1)
def down(self,kwargs):
    print kwargs
    url = kwargs.get("res")
    key = hashlib.md5(url.encode("utf8")).hexdigest()
    response = requests.get(url)
    response = scrapy.Selector(text=response.content,type="html")
    url = response.xpath("//iframe/@src").extract_first()
    response = requests.get(url)
    url = re.search(r"src\":\"(\S*?)\"",response.content).groups(0)[0]
    url = "http:" + url.replace("\\","")
    response = requests.get(url)
    if response.url.find("now_printing") < 0:
        ext = response.url.split(".")[-1]
        # with open("/mnt/dmmvideo/{}.{}".format(key,ext), "wb") as code:
        #     code.write(response.content)
        # ret = client.upload_by_buffer(response.content,ext)
        bucket.put_object(key + "." + ext, response.content)
        # print ret
        # item = {}
        # item["url"] = kwargs.get("url")
        # item["type"] = kwargs.get("type")
        # item["source"] = kwargs.get("source")
        # item["path"] = ret["Remote file_id"]
        # item["date"] = str(date.today())
        return kwargs.get("url")


    # with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    #     ydl.download([url])

# for x in client.xvideo.video.find({"status":{"$exists":False}},{"url":1},no_cursor_timeout=True).limit(3):
#     print (x["url"])
#     down,delay(x["url"])
# @app.on_after_configure.connect
# def setup_periodic_tasks(sender, **kwargs):
#     for x in client.xvideo.video.find({"status":{"$exists":False}},{"url":1},no_cursor_timeout=True).limit(3):
        # import ipdb;ipdb.set_trace()
        # print (x["url"])
        # down,delay(x["url"])
        # sender.add_periodic_task(timedelta(minutes=1), down.s(x["url"]), name='xvideo',queue='xvideo')
        # sender.add_periodic_task(timedelta(minutes=0.5), down.s(x["url"]), name='xvideo',queue='xvideo')